# @Time    :2025/4/5 18:14
import re
import time
from py.day12后的清明假期.day12后的清明假期.客户信息管理系统.P02客户类 import Customer


class CMS:
    """客户管理系统类"""

    def __init__(self):
        """"初始化客户管理系统"""
        self.customer_id_dict = {}
        self.customer_name_dict = {}

    def menus(self):
        """主菜单"""
        print('''--------------客户管理系统-----------
                                1.添加客户
                                2.删除客户
                                3.修改客户
                                4.查询客户
                                5.显示客户
                                6.退出''')

    def add_customer_id(self):
        """添加客户，id校验"""
        customer_id = "None"
        for i in range(3):
            if i < 2:
                customer_id = input("请输入客户id:")
                if Customer.check_id(customer_id):
                    print(customer_id)
                    break
                else:
                    print("id必须为纯数字!")
            else:
                customer_id = input("最后一次机会，请重新输入：")
                if Customer.check_id(customer_id):
                    break
                else:
                    print("终止添加客户")
                    return False
        if customer_id in self.customer_id_dict:
            print("客户id已存在，终止添加客户")
            return False
        else:
            return customer_id

    def add_customer_name(self):
        """添加客户，name校验"""
        customer_name = "None"
        for i in range(3):
            if i < 2:
                customer_name = input("请输入客户name:")
                if Customer.check_name(customer_name):
                    print(customer_name)
                    break
                else:
                    print("名字必须为字母或汉字!")
            else:
                customer_name = input("最后一次机会，请重新输入：")
                if Customer.check_name(customer_name):
                    break
                else:
                    print("终止添加客户")
                    return False
        return customer_name

    def add_customer_age(self):

        customer_age = input("请输入你的年龄：")
        if Customer.check_age(customer_age):
            return customer_age
        else:
            print("输入的age不合理，使用默认的")
        return "None"  # 这里要写返回值，不写的话， 这里默认返回的 是None 会覆盖掉初始化设置的默认的“None”，会报错

    def add_customer_phone(self):
        customer_phone = input("请输入客户的电话：")
        if Customer.check_phone(customer_phone):
            return customer_phone
        elif re.search(r"^[\d-]+$", customer_phone):
            print("这个号码不常见，但是可以添加")
            return customer_phone
        else:
            print("好吧，暂时不添加也可也")
        return "None"

    def add_customer_email(self):
        customer_email = input("请输入客户的邮箱：")
        if Customer.check_email(customer_email):
            return customer_email
        else:
            print("好吧，暂时不添加也可也")
        return "None"

    def add_customer(self):
        """添加客户"""
        # 添加客户id
        if not (customer_id := self.add_customer_id()):
            return
        # 添加客户姓名
        if not (customer_name := self.add_customer_name()):
            return
        # 添加客户年龄
        customer_age = self.add_customer_age()
        # 添加客户电话
        customer_phone = self.add_customer_phone()
        # 添加客户邮箱
        customer_email = self.add_customer_email()
        # 添加客户对象
        customer = Customer(customer_id, customer_name, customer_age, customer_phone, customer_email)
        # 将客户对象添加到字典里
        customer_inner_dict = self.customer_name_dict.get(customer_name)
        if customer_inner_dict is None:
            self.customer_name_dict[customer_name] = {customer_id: customer}
        else:
            customer_inner_dict[customer_id] = customer
        print(f"添加客户{customer_id}成功")

    def delete_customer(self):
        """删除客户"""
        # 获取客户的id
        customer_id = input("请输入要删除的客户id：")

        # 检查客户id 是否合法
        if not Customer.check_id(customer_id):
            print("客户id必须是纯数字，终止删除客户")
            return
            # 检查id是否存在
        if customer_id not in self.customer_id_dict:
            print("客户id不存在，终止删除id")
            return
        else:
            customer_name = self.customer_name_dict[customer_id]
            # 将客户id从客户id字典里删除
            del self.customer_id_dict[customer_id]
            # 将客户id从客户名字字典里删除
            customer_inner_dict = self.customer_name_dict.get(customer_name)
            if customer_inner_dict is not None:
                del customer_inner_dict[customer_id]
            print(f"客户{customer_id}删除完毕")

    def update_customer(self):
        """更新客户"""
        # 获取客户的id
        customer_id = input("请输入要更新的客户id：")

        # 检查客户id 是否合法
        if not Customer.check_id(customer_id):
            print("客户id必须是纯数字，终止添加客户")
            return
            # 检查id是否存在
        if customer_id not in self.customer_id_dict:
            print("客户id不存在，终止添加id")
            return

        # 修改客户年龄
        print(f"客户{customer_id}的历史年龄:", self.customer_id_dict[customer_id].age)
        if (customer_age := self.set_customer_age()) != "None":
            self.customer_id_dict[customer_id].age = customer_age
        # 修改客户电话
        print(f"客户{customer_id}的历史电话:", self.customer_id_dict[customer_id].phone)
        if (customer_phone := self.set_customer_phone()) != "None":
            self.customer_id_dict[customer_id].phone = customer_phone
        # 修改客户邮箱
        print(f"客户{customer_id}的历史邮箱:", self.customer_id_dict[customer_id].email)
        if (customer_email := self.set_customer_email()) != "None":
            self.customer_id_dict[customer_id].email = customer_email
        print(f"客户{customer_id}修改完毕")

    def search_customer(self):
        """查询客户"""
        customer_info = input("请输入要查询的客户id或姓名:")
        if Customer.check_id(customer_info):
            # 如果输入的是id
            # 检查客户id是否存在
            if customer_info in self.customer_id_dict:
                print(self.customer_id_dict[customer_info])
            else:
                print("客户id不存在")
        elif Customer.check_name(customer_info):
            # 如果输入的是姓名
            # 检查客户姓名是否存在
            if customer_info in self.customer_name_dict:
                for customer_id in self.customer_name_dict[customer_info]:
                    print(self.customer_name_dict[customer_info][customer_id])
            else:
                print("客户姓名不存在")
        else:
            print("输入的好像不是客户id或姓名")

    def search_customer(self):
        """查询客户"""
        # 获取客户的id
        customer_info = input("请输入要查询的客户id或者名字：")

        # 检查客户id 是否合法
        if Customer.check_id(customer_info):
            # 检查id是否存在
            if customer_info in self.customer_id_dict:
                print(self.customer_id_dict[customer_info])
            else:
                print("客户id不存在")
        elif Customer.check_name(customer_info):
            if customer_info in self.customer_name_dict:
                for customer_id in self.customer_name_dict[customer_info]:
                    print(self.customer_name_dict[customer_info][customer_id])
            else:
                print("客户姓名不存在")
        else:
            print("输入的好像不是客户id或者姓名")

    def display_customer(self):
        """打印客户信息"""
        if len(self.customer_id_dict) == 0:
            print("暂无客户信息")
        for i in self.customer_id_dict:
            print(self.customer_id_dict[i])

    def start(self):
        """启动客户管理系统 ，进入到菜单页面"""
        try:
            while True:
                # 显示菜单
                self.menus()
                choice = input("<< ")
                match choice:
                    case "1":
                        self.add_customer()
                    case "2":
                        self.delete_customer()
                    case "3":
                        self.update_customer()
                    case "4":
                        self.search_customer()
                    case "5":
                        self.display_customer()
                    case "6":
                        print("退出客户系统")
                        time.sleep(0.1)
                    case _:
                        print("输入有误，请重新输入：")

        except Exception as e:
            print(e)


if __name__ == "__main__":
    cms = CMS()
    cms.start()
